﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JVP.Infrastructure;
using System.Configuration;
using System.Data.SqlClient;

namespace JVP.Gateway
{    
    public class Gateway : IGateway 
    {
        string connectionString = ConfigurationSettings.AppSettings["ConnectionString"].ToString();
        SqlCommand sqlCmd;

        #region IGateway Members
        /// <summary>
        /// To check the user id and password of the user
        /// </summary>
        /// <param name="userName"><see cref="String"/> that contains user id of the user</param>
        /// <param name="passWord"><see cref="String"/> that contains password of the user</param>
        /// <returns><see cref="Boolean"/> that returns true or false value based on the query execution</returns>
        public bool isValidUser(string userName, string passWord)
        {
            IDBManager dbManager = ObjectManager.GetObject<IDBManager>();
            sqlCmd = dbManager.getCommand(connectionString);
            IOfficeManager officeManager = ObjectManager.GetObject<IOfficeManager>();
            bool res= officeManager.isValidUser(userName, passWord, sqlCmd);
            dbManager.closeConnection();
            return res;            
        }

        #endregion

        #region IGateway Members

        /// <summary>
        /// To get available plans from Database which in active status
        /// </summary>
        /// <returns><see cref="System.Data.SqlClient"/>that returns the data adapter of the result</returns>

        public SqlDataReader getPlans()
        {
            IDBManager dbManager = ObjectManager.GetObject<IDBManager>();
            sqlCmd = dbManager.getCommand(connectionString);
            IOfficeManager officeManager = ObjectManager.GetObject<IOfficeManager>();
            SqlDataReader dr = officeManager.getPlans(sqlCmd);
            return dr;
        }

        #endregion

        #region IGateway Members

        /// <summary>
        /// To add a new plan into the database
        /// </summary>
        /// <param name="planName"><see cref="System.String"/> that holds the name of the plan</param>
        /// <param name="planTerm"><see cref="System.String"/> that holds the term of the plan</param>
        /// <param name="planType"><see cref="System.String"/> that holds the type of the plan</param>
        /// <param name="description"><see cref="System.String"/> that holds the description about the plan</param>
        /// <returns>it returns true when the command executed correctly otherwise returns false</returns>

        public bool addPlan(string planName, string planTerm, string planType, string planStatus, string description)
        {
            IDBManager dbManager = ObjectManager.GetObject<IDBManager>();
            sqlCmd = dbManager.getCommand(connectionString);
            IOfficeManager officeManager = ObjectManager.GetObject<IOfficeManager>();
            bool res = officeManager.addPlan(planName, planTerm, planType, planStatus, description, sqlCmd);
            return res;
        }

        #endregion

        #region IGateway Members

        /// <summary>
        /// To get the plan details for the particular plan id
        /// </summary>
        /// <param name="planID"><see cref="System.String"/> that holds the id of the plan</param>
        /// <returns>the result of the plan details</returns>

        public SqlDataReader getPlan(string planID)
        {
            IDBManager dbManager = ObjectManager.GetObject<IDBManager>();
            sqlCmd = dbManager.getCommand(connectionString);
            IOfficeManager officeManager = ObjectManager.GetObject<IOfficeManager>();
            SqlDataReader dr = officeManager.getPlan(planID,sqlCmd);
            return dr;
        }

        #endregion

        #region IGateway Members

        /// <summary>
        /// To Update the Plan Details
        /// </summary>
        /// <param name="planID"><see cref="System.String"/> that holds the id of the plan</param>
        /// <param name="planName"><see cref="System.String"/> that holds the name of the plan</param>
        /// <param name="planTerm"><see cref="System.String"/> that holds the term of the plan</param>
        /// <param name="description"><see cref="System.String"/> that holds the description of the plan</param>
        /// <returns><see cref="System.Boolean"/> returns true when query executed successfully otherwise false.</returns>

        public bool editPlan(string planID, string planName, string planTerm, string planType, string planStatus, string description)
        {
            IDBManager dbManager = ObjectManager.GetObject<IDBManager>();
            sqlCmd = dbManager.getCommand(connectionString);
            IOfficeManager officeManager = ObjectManager.GetObject<IOfficeManager>();
            bool res=officeManager.editPlan(planID, planName, planTerm, planType, planStatus, description, sqlCmd);
            return res;
        }

        #endregion

        #region IGateway Members

        /// <summary>
        /// To add a new scheme into the database
        /// </summary>
        /// <param name="schemeDues"><see cref="System.Int32"/> that holds the number of the dues in that scheme</param>
        /// <param name="planYear"><see cref="System.String"/> that holds the duration of the scheme</param>
        /// <param name="amtInstall"><see cref="System.String"/> that holds the installment amount of the scheme</param>
        /// <param name="amtConsi"><see cref="System.String"/> that holds the consideration amount of the scheme</param>
        /// <param name="amtExpConsi"><see cref="System.String"/> that holds the expected consideration of the scheme</param>
        /// <param name="amtAccConsi"><see cref="System.String"/> that holds the accedential consideration of the scheme</param>
        /// <param name="plotSize"><see cref="System.String"/> that holds the size of the plot</param>
        /// <param name="schemeStatus"><see cref="System.String"/> that holds current status of the scheme</param>
        /// <param name="planID"><see cref="System.String"/> that holds the id of the plan</param>
        /// <returns>It returns true when the query executed correctly otherwise returns false.</returns>
        
        public bool addScheme(int schemeDues, string planYear, string amtInstall, string amtConsi, string amtExpConsi, string amtAccConsi, string plotSize, string schemeStatus, string planID)
        {
            IDBManager dbManager = ObjectManager.GetObject<IDBManager>();
            sqlCmd = dbManager.getCommand(connectionString);
            IOfficeManager officeManager = ObjectManager.GetObject<IOfficeManager>();
            bool res = officeManager.addScheme(schemeDues, planYear, amtInstall, amtConsi, amtExpConsi, amtAccConsi, plotSize, schemeStatus, planID, sqlCmd);
            return res;
        }

        #endregion

        #region IGateway Members

        /// <summary>
        /// To get all schemes from database
        /// </summary>
        /// <returns></returns>

        public SqlDataAdapter getSchemes()
        {
            IDBManager dbManager = ObjectManager.GetObject<IDBManager>();
            sqlCmd = dbManager.getCommand(connectionString);
            IOfficeManager officeManager = ObjectManager.GetObject<IOfficeManager>();
            SqlDataAdapter da = officeManager.getSchemes(sqlCmd);
            return da;
        }

        #endregion

        #region IGateway Members

        /// <summary>
        /// To get the branch details from database.
        /// </summary>
        /// <returns></returns>

        public SqlDataReader getBranches()
        {
            IDBManager dbManager = ObjectManager.GetObject<IDBManager>();
            sqlCmd = dbManager.getCommand(connectionString);
            IOfficeManager officeManager = ObjectManager.GetObject<IOfficeManager>();
            SqlDataReader dr = officeManager.getBranches(sqlCmd);
            return dr;
        }

        #endregion

        #region IGateway Members

        /// <summary>
        /// To get the branch detail of the particular branch id.
        /// </summary>
        /// <param name="branchID"><see cref="System.String"/> that holds the id of the branch</param>
        /// <returns></returns>

        public SqlDataReader getBranch(string branchID)
        {
            IDBManager dbManager = ObjectManager.GetObject<IDBManager>();
            sqlCmd = dbManager.getCommand(connectionString);
            IOfficeManager officeManager = ObjectManager.GetObject<IOfficeManager>();
            SqlDataReader dr = officeManager.getBranch(branchID, sqlCmd);
            return dr;            
        }

        #endregion

        #region IGateway Members

        /// <summary>
        /// To add new branch into the database
        /// </summary>
        /// <param name="branchCode"><see cref="System.String"/> that holds the code of the branch</param>
        /// <param name="branchName"><see cref="System.String"/> that holds the name of the branch</param>
        /// <param name="branchCity"><see cref="System.String"/> that holds the city name of the branch</param>
        /// <param name="branchCCode"><see cref="System.String"/> that holds the city STD code of the branch</param>        
        /// <returns>true if the query executed correctly else returns false</returns>

        public bool addBranch(string branchCode, string branchName, string branchCity, string branchCCode)
        {
            IDBManager dbManager = ObjectManager.GetObject<IDBManager>();
            sqlCmd = dbManager.getCommand(connectionString);
            IOfficeManager officeManager = ObjectManager.GetObject<IOfficeManager>();
            bool res = officeManager.addBranch(branchCode, branchName, branchCity, branchCCode, sqlCmd);
            return res;
        }

        #endregion

        #region IGateway Members

        /// <summary>
        /// To create a new user account
        /// </summary>
        /// <param name="fName"><see cref="System.String"/> that holds first name of the employee</param>
        /// <param name="lName"><see cref="System.String"/> that holds last name of the employee</param>
        /// <param name="dob"><see cref="System.DateTime"/> that holds date of birth of the employee</param>
        /// <param name="empAddress"><see cref="System.String"/> that holds address of the employee</param>
        /// <param name="empMobile"><see cref="System.String"/> that holds mobile number of the employee</param>
        /// <param name="empStatus"><see cref="System.String"/> that holds current status of the employee</param>
        /// <param name="branchID"><see cref="System.Int32"/> that holds id of the branch</param>
        /// <returns></returns>

        public bool createAccount(string fName, string lName, DateTime dob, string empAddress, string empMobile, string empStatus, int branchID)
        {
            IDBManager dbManager = ObjectManager.GetObject<IDBManager>();
            sqlCmd = dbManager.getCommand(connectionString);
            IOfficeManager officeManager = ObjectManager.GetObject<IOfficeManager>();
            bool res = officeManager.createAccount(fName, lName, dob, empAddress, empMobile, empStatus, branchID, sqlCmd);
            return res;
        }

        #endregion

        #region IGateway Members

        /// <summary>
        /// To Change Password for an user
        /// </summary>
        /// <param name="userName"><see cref="System.String"/> that holds the userid of the account holder</param>
        /// <param name="oldPass"><see cref="System.String"/> that holds the existing password of the user</param>
        /// <param name="newPass"><see cref="System.String"/> that holds the new password of the user</param>
        /// <returns></returns>

        public bool changePassword(string userName, string oldPass, string newPass)
        {
            IDBManager dbManager = ObjectManager.GetObject<IDBManager>();
            sqlCmd = dbManager.getCommand(connectionString);
            IOfficeManager officeManager = ObjectManager.GetObject<IOfficeManager>();
            bool res = officeManager.changePassword(userName, oldPass, newPass, sqlCmd);
            return res;
        }

        #endregion

        #region IGateway Members

        /// <summary>
        /// To get scheme details for a particular plan id.
        /// </summary>
        /// <param name="planID"><see cref="System.String"/> that holds the id of the plan</param>
        /// <returns></returns>

        public SqlDataReader getScheme(string planID)
        {
            IDBManager dbManager = ObjectManager.GetObject<IDBManager>();
            sqlCmd = dbManager.getCommand(connectionString);
            IOfficeManager officeManager = ObjectManager.GetObject<IOfficeManager>();
            SqlDataReader dr = officeManager.getScheme(planID, sqlCmd);
            return dr;
        }

        #endregion

        #region IGateway Members

        /// <summary>
        /// To get available plans from Database
        /// </summary>
        /// <returns><see cref="System.Data.SqlClient"/>that returns the data adapter of the result</returns>

        public SqlDataReader getAllPlans()
        {
            IDBManager dbManager = ObjectManager.GetObject<IDBManager>();
            sqlCmd = dbManager.getCommand(connectionString);
            IOfficeManager officeManager = ObjectManager.GetObject<IOfficeManager>();
            SqlDataReader dr = officeManager.getAllPlans(sqlCmd);
            return dr;
        }

        #endregion

        #region IGateway Members

        /// <summary>
        /// To edit the existing scheme into the database
        /// </summary>
        /// <param name="schemeID"><see cref="System.String"/> that holds the id of the scheme</param>
        /// <param name="schemeDues"><see cref="System.Int32"/> that holds the number of the dues in that scheme</param>
        /// <param name="planYear"><see cref="System.String"/> that holds the duration of the scheme</param>
        /// <param name="amtInstall"><see cref="System.String"/> that holds the installment amount of the scheme</param>
        /// <param name="amtConsi"><see cref="System.String"/> that holds the consideration amount of the scheme</param>
        /// <param name="amtExpConsi"><see cref="System.String"/> that holds the expected consideration of the scheme</param>
        /// <param name="amtAccConsi"><see cref="System.String"/> that holds the accedential consideration of the scheme</param>
        /// <param name="plotSize"><see cref="System.String"/> that holds the size of the plot</param>
        /// <param name="schemeStatus"><see cref="System.String"/> that holds current status of the scheme</param>
        /// <param name="planID"><see cref="System.String"/> that holds the id of the plan</param>
        /// <returns>It returns true when the query executed correctly otherwise returns false.</returns>
        
        public bool editScheme(string schemeID, int schemeDues, string planYear, string amtInstall, string amtConsi, string amtExpConsi, string amtAccConsi, string plotSize, string schemeStatus, string planID)
        {
            IDBManager dbManager = ObjectManager.GetObject<IDBManager>();
            sqlCmd = dbManager.getCommand(connectionString);
            IOfficeManager officeManager = ObjectManager.GetObject<IOfficeManager>();
            bool res = officeManager.editScheme(schemeID, schemeDues, planYear, amtInstall, amtConsi, amtExpConsi, amtAccConsi, plotSize, schemeStatus, planID, sqlCmd);
            return res;
        }

        #endregion

        #region IGateway Members

        /// <summary>
        /// To add new scheme into the database
        /// </summary>
        /// <param name="designationName"><see cref="System.String"/> that holds the name of the designation</param>
        /// <param name="rank"><see cref="System.Int32"/> that holds the rank of the particular designation</param>
        /// <param name="firstYear"><see cref="System.Decimal"/> that holds the percentage of the first year commission for the designation</param>
        /// <param name="secondYear"><see cref="System.Decimal"/> that holds the percentage of the second year commission for the designation</param>
        /// <param name="thirdYear"><see cref="System.Decimal"/> that holds the percentage of the third year commission for the designation</param>
        /// <param name="lumpSum"><see cref="System.Decimal"/> that holds the percentage of the lump sum commission for the designation</param>
        /// <returns></returns>


        public bool addDesignation(string designationName, int rank, decimal firstYear, decimal secondYear, decimal thirdYear, decimal lumpSum)
        {
            IDBManager dbManager = ObjectManager.GetObject<IDBManager>();
            sqlCmd = dbManager.getCommand(connectionString);
            IOfficeManager officeManager = ObjectManager.GetObject<IOfficeManager>();
            bool res = officeManager.addDesignation(designationName,rank,firstYear,secondYear,thirdYear,lumpSum,sqlCmd);
            return res;
        }

        #endregion

        #region IGateway Members

        /// <summary>
        /// To get available designations from database
        /// </summary>
        /// <returns></returns>

        public SqlDataAdapter getDesignations()
        {
            IDBManager dbManager = ObjectManager.GetObject<IDBManager>();
            sqlCmd = dbManager.getCommand(connectionString);
            IOfficeManager officeManager = ObjectManager.GetObject<IOfficeManager>();
            SqlDataAdapter da = officeManager.getDesignations(sqlCmd);
            return da;
        }

        #endregion

        #region IGateway Members

        /// <summary>
        /// To edit the designation details into the database
        /// </summary>
        /// <param name="designationID"><see cref="System.Int32"/> that holds the id of the designation</param>
        /// <param name="designationName"><see cref="System.String"/> that holds the name of the designation</param>
        /// <param name="rank"><see cref="System.Int32"/> that holds the rank of the designation</param>
        /// <param name="firstYear"><see cref="System.Decimal"/> that holds the percentage of the first year for the particular designation</param>
        /// <param name="secondYear"><see cref="System.Decimal"/> that holds the percentage of the second year for the particular designation</param>
        /// <param name="thirdYear"><see cref="System.Decimal"/> that holds the percentage of the third year for the particular designation</param>
        /// <param name="lumpSum"><see cref="System.Decimal"/> that holds the percentage of the lump sum for the particular designation</param>
        /// <returns></returns>

        public bool editDesignation(int designationID, string designationName, int rank, decimal firstYear, decimal secondYear, decimal thirdYear, decimal lumpSum)
        {
            IDBManager dbManager = ObjectManager.GetObject<IDBManager>();
            sqlCmd = dbManager.getCommand(connectionString);
            IOfficeManager officeManager = ObjectManager.GetObject<IOfficeManager>();
            bool res = officeManager.editDesignation(designationID, designationName, rank, firstYear, secondYear, thirdYear, lumpSum, sqlCmd);
            return res;
        }

        #endregion

        #region IGateway Members


        public SqlDataAdapter getDownLine()
        {
            IDBManager dbManager = ObjectManager.GetObject<IDBManager>();
            sqlCmd = dbManager.getCommand(connectionString);
            IOfficeManager officeManager = ObjectManager.GetObject<IOfficeManager>();
            SqlDataAdapter da = officeManager.getDownLine(sqlCmd);
            return da;
        }

        #endregion

        #region IGateway Members


        public SqlDataAdapter getInformations(string designID)
        {
            IDBManager dbManager = ObjectManager.GetObject<IDBManager>();
            sqlCmd = dbManager.getCommand(connectionString);
            IOfficeManager officeManager = ObjectManager.GetObject<IOfficeManager>();
            SqlDataAdapter da = officeManager.getInformations(designID, sqlCmd);
            return da;
        }

        #endregion

        #region IGateway Members


        public SqlDataAdapter getCustomers(string designID)
        {
            IDBManager dbManager = ObjectManager.GetObject<IDBManager>();
            sqlCmd = dbManager.getCommand(connectionString);
            IOfficeManager officeManager = ObjectManager.GetObject<IOfficeManager>();
            SqlDataAdapter da = officeManager.getCustomers(designID, sqlCmd);
            return da;
        }

        #endregion

        #region IGateway Members


        public SqlDataReader getUpline(string designID)
        {
            IDBManager dbManager = ObjectManager.GetObject<IDBManager>();
            sqlCmd = dbManager.getCommand(connectionString);
            IOfficeManager officeManager = ObjectManager.GetObject<IOfficeManager>();
            SqlDataReader dr = officeManager.getUpline(designID, sqlCmd);
            return dr;
        }

        #endregion

        #region IGateway Members

        /// <summary>
        /// 
        /// </summary>
        /// <param name="schemeID"></param>
        /// <param name="cssNo"></param>
        /// <param name="pName"></param>
        /// <param name="pAge"></param>
        /// <param name="pGender"></param>
        /// <param name="fatherOf"></param>
        /// <param name="rName"></param>
        /// <param name="nName"></param>
        /// <param name="nAge"></param>
        /// <param name="nRelation"></param>
        /// <param name="pAddress"></param>
        /// <param name="dateComm"></param>
        /// <param name="agentCode"></param>
        /// <param name="upLine"></param>
        /// <param name="userID"></param>
        /// <returns></returns>

        public bool addPolicy(int schemeID, int cssNo, string pName, string pAge, string pGender, string fatherOf, string rName, string nName, string nAge, string nRelation, string pAddress, DateTime dateComm, int agentCode, System.Data.DataSet upLine, string userID, int branchID)
        {
            IDBManager dbManager = ObjectManager.GetObject<IDBManager>();
            sqlCmd = dbManager.getCommand(connectionString);
            IUserManager userManager = ObjectManager.GetObject<IUserManager>();
            bool res = userManager.addPolicy(schemeID, cssNo, pName, pAge, pGender, fatherOf, rName, nName, nAge, nRelation, pAddress, dateComm, agentCode, upLine, userID, branchID, sqlCmd);
            return res;
        }

        #endregion

        #region IGateway Members

        /// <summary>
        /// To get the details of the office staff..
        /// </summary>
        /// <param name="userID"><see cref="System.String"/> that holds the office staff user ID</param>
        /// <returns><see cref="System.SqlClient"/> that returns the sql data reader of office staff details</returns>

        public SqlDataReader getOfficeStaffDetails(string userID)
        {
            IDBManager dbManager = ObjectManager.GetObject<IDBManager>();
            sqlCmd = dbManager.getCommand(connectionString);
            IOfficeManager officeManager = ObjectManager.GetObject<IOfficeManager>();
            SqlDataReader dr = officeManager.getOfficeStaffDetails(userID, sqlCmd);
            return dr;
        }

        #endregion

        #region IGateway Members


        public SqlDataAdapter getRankList()
        {
            IDBManager dbManager = ObjectManager.GetObject<IDBManager>();
            sqlCmd = dbManager.getCommand(connectionString);
            IEmployeeManager empManager = ObjectManager.GetObject<IEmployeeManager>();
            SqlDataAdapter da = empManager.getRankList(sqlCmd);
            return da;
        }

        #endregion
    }
}
